﻿//////////////////////////////////////////////////////////////////
//
// ConverterBase.cs
//
// Copyright (c) 2012 Dan Pike.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;

namespace Interfaces
{
   public abstract class ConverterBase<T> : TypeConverter
      where T : class
   {
      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Tell the system that we can convert to/from strings
      /// </summary>
      /// <param name="context"></param>
      /// <param name="sourceType"></param>
      /// <returns></returns>
      public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
      {
         return (typeof(string) == sourceType);
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Tell the system that we can convert to/from strings
      /// </summary>
      /// <param name="context"></param>
      /// <param name="sourceType"></param>
      /// <returns></returns>
      public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
      {
         return (typeof(string) == destinationType);
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Override to convert from a string to an object of the given type
      /// </summary>
      /// <param name="value"></param>
      /// <returns></returns>
      protected abstract T FromString(string value);

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Override to convert from an object of the given type to a string
      /// </summary>
      /// <param name="value"></param>
      /// <returns></returns>
      protected abstract string ToString(T value);

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// We only know how to convert to/from strings for the configuration files
      /// </summary>
      /// <param name="context"></param>
      /// <param name="culture"></param>
      /// <param name="value"></param>
      /// <returns></returns>
      public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
      {
         return FromString(value as string);
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// We only know how to convert to/from strings for the configuration files
      /// </summary>
      /// <param name="context"></param>
      /// <param name="culture"></param>
      /// <param name="value"></param>
      /// <returns></returns>
      public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
      {
         return ToString(value as T);
      }

      ////////////////////////////////////////////////////////////////////////
      // ot sure if we need to override these, too?
      //public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues) { return null; }
      //public override bool GetCreateInstanceSupported(ITypeDescriptorContext context) { return false; }
      //public override bool GetPropertiesSupported(ITypeDescriptorContext context) { return false; }
      //public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return null; }
      //public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return false; }
      //public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return false; }
      //public override bool IsValid(ITypeDescriptorContext context, object value) { return false;  }
   }
}
